package bai3.com.client;

/**
 * @author nglehien
 * Oct 21, 2012 - 8:14:20 PM
 */
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.NotBoundException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import bai3.com.server.BankSystem;
import bai3.com.server.BankSystemInterface;
import bai3.com.util.Account;


public class ClientGUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtUsername;
	private JTextField txtPasswrod;
	private JButton btnRegister;
	private JButton btnSignIn;
	private JLabel lblPassword;
	private JLabel lblUsername;
	private BankSystemInterface bankSystem;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientGUI frame = new ClientGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClientGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 345, 171);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lblUsername = new JLabel("Username");
		lblUsername.setFont(new Font("Times New Roman", Font.PLAIN, 15));
		lblUsername.setBounds(10, 13, 81, 14);
		contentPane.add(lblUsername);

		lblPassword = new JLabel("Password:");
		lblPassword.setFont(new Font("Times New Roman", Font.PLAIN, 15));
		lblPassword.setBounds(10, 38, 63, 14);
		contentPane.add(lblPassword);

		txtUsername = new JTextField();
		txtUsername.setBounds(100, 11, 192, 20);
		contentPane.add(txtUsername);
		txtUsername.setColumns(10);

		txtPasswrod = new JTextField();
		txtPasswrod.setColumns(10);
		txtPasswrod.setBounds(100, 36, 192, 20);
		contentPane.add(txtPasswrod);

		btnSignIn = new JButton("Sign In");
		btnSignIn.setBounds(60, 79, 89, 23);
		contentPane.add(btnSignIn);
		btnSignIn.addActionListener(onClickSignIn);
		btnRegister = new JButton("Register");
		btnRegister.addActionListener(onClickRegister);
		btnRegister.setBounds(178, 79, 89, 23);
		contentPane.add(btnRegister);
	}

	private ActionListener onClickRegister = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				bankSystem = (BankSystemInterface) Naming
						.lookup(BankSystem.BANK_SERVER);
				Account account = new Account(txtUsername.getText(),
						txtPasswrod.getText());
				if (bankSystem.register(account)) {
					JOptionPane.showMessageDialog(ClientGUI.this,
							"Success!");
				}
			} catch (NotBoundException | IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	};
	private ActionListener onClickSignIn = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				bankSystem = (BankSystemInterface) Naming
						.lookup(BankSystem.BANK_SERVER);
				Account account = new Account(txtUsername.getText(),
						txtPasswrod.getText());
				if (bankSystem.signIn(account)) {
					JOptionPane.showMessageDialog(ClientGUI.this,
							"Success!");
				}
			} catch (NotBoundException | IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	};
}
